#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_NUM 1000

struct DATA_TYPE {
	int deleted;				//0表示正常 ，1表示该记录已删除
	int price;
	char No[20];				// 商品编号
	char Name[80];				//商品名称
	char ProducerName[20];		//产家名字
	char ProducedDate[20];		//生产日期
	char StockInDate[20];		//入库日期
	char HandlerName[20];			//经手人名字
	int  inventory;				//库存
};


int  ReadFromFile(struct DATA_TYPE*);
int  WriteToFile(struct DATA_TYPE*, int &num_used);
int  QueryByNo(struct DATA_TYPE*, char No[], int &num_used);
void SumPrice(struct DATA_TYPE*, int &num_used); 
int Stock_in(struct DATA_TYPE*, int &num_used);
void Stock_out(struct DATA_TYPE*, int& num_used);
int Stock_list(struct DATA_TYPE*, int &num_used);

int main()
{
	struct DATA_TYPE* pDatabase = (struct DATA_TYPE*) malloc(sizeof(struct DATA_TYPE) * MAX_NUM);//动态分配内存

	if (pDatabase == NULL) //如果返回NULL，则申请空间不成功
	{
		printf("Error Found\n");
		return -1;
	}

	int numOfgoods = ReadFromFile(pDatabase);

	printf("**************************************************************************************************\n");
	printf("*                                                                                                *\n");
	printf("*                                                                                                *\n");
	printf("*                                欢迎来到派大星的仓库管理系统                                    *\n");
	printf("*                                                                                                *\n");
	printf("*                                                                                                *\n");
	printf("*                                 1. 商品入库                                                    *\n");
	printf("*                                                                                                *\n");
	printf("*                                 2. 商品库库列表显示                                            *\n");
	printf("*                                                                                                *\n");
	printf("*                                 3. 商品入库                                                    *\n");
	printf("*                                                                                                *\n");
	printf("*                                 4. 统计所有货物的价格                                          *\n");
	printf("*                                                                                                *\n");
	printf("*                                 0. 退出程序                                                    *\n");
	printf("*                                                                                                *\n");
	printf("**************************************************************************************************\n");

	

	while (true)
	{
		printf("请输入要执行的操作:\n");
		char whichMode = _getch();

		switch (whichMode)
		{
		case '1':
			Stock_in(pDatabase, numOfgoods);
			break;
		case '2':
			Stock_list(pDatabase, numOfgoods);
			break;
		case '3':
			Stock_out(pDatabase, numOfgoods);
			break;
		default:
			break;
		}

		if (whichMode == '0')
		{
			WriteToFile(pDatabase, numOfgoods);
			free(pDatabase);
			return 0;
		}
	}
}

int  ReadFromFile(struct DATA_TYPE* pDataBase) 
{
	FILE* fp;
	fp = fopen("d:\\mydatabase.dat", "rb");
	if (fp == NULL) 
	{
		return 0;
	}
	int num = 0;

	while ((fread((void*)&pDataBase[num], sizeof(struct DATA_TYPE), 1, fp) == 1)) {
		pDataBase[num].deleted = 0;
		num++;
	}
	fclose(fp);
	return num;
}

int  WriteToFile(struct DATA_TYPE* pDataBase, int &num_used) 
{
	FILE* fp;
	fp = fopen("d:\\mydatabase.dat", "wb");
	if (fp == NULL) 
	{
		printf("Error\n");
		return -1;
	}

	for (int i = 0; i < num_used; i++) 
	{
		if (pDataBase[i].deleted == 0)  //清理已删除的数据
			fwrite(&pDataBase[i], sizeof(struct DATA_TYPE), 1, fp);
	}

	fclose(fp);
	return 0;
}

int  QueryByNo(struct DATA_TYPE* pDataBase, char No[], int &num_used) 
{
	//**** (2)**** 如果要提高查询效率，需要对数据进行什么处理？需要采用什么算法

	int num = -1;
	for (int i = 0; i < num_used; i++) 
	{
		// No是用于查询的关键，查到返回记录，找不到返回-1
		if (!strcmp(pDataBase[i].No, No)) 
		{
			num = i;
			break;
		}
	}
	return num;
}

//入库操作
int Stock_in(struct DATA_TYPE* pDataBase, int &num_used) 
{
	while (true)
	{
		printf("请输入货物号:\n");
		scanf("%s", pDataBase[num_used].No);

		printf("请输入入货量:\n");
		int inventory = 0;
		scanf("%d", &inventory);

		if (QueryByNo(pDataBase, pDataBase[num_used].No, num_used) != -1)
		{
			printf("已经有相同货物号记录,将在更改库存量后退出\n");
			pDataBase[num_used].inventory += inventory;
		}
		else
		{
			pDataBase[num_used].inventory = inventory;
			printf("请输入商品名称:\n");
			scanf("%s", pDataBase[num_used].Name);

			printf("请输入产家名称:\n");
			scanf("%s", pDataBase[num_used].ProducerName);

			printf("请输入商品价格:\n");
			scanf("%s", &pDataBase[num_used].price);

			printf("请输入生产日期:\n");
			scanf("%s", pDataBase[num_used].ProducedDate);

			printf("请输入入库日期:\n");
			scanf("%s", pDataBase[num_used].StockInDate);

			printf("请输经手人名字:\n");
			scanf("%s", pDataBase[num_used].HandlerName);

			pDataBase[num_used].deleted = 0;

			num_used++;
		}

		printf("货物信息录入成功，要想输入继续输入请按1，按其他退出:\n");
		char jude = _getch();

		if (jude != '1')
		{
			break;
		}
		
	}
	return num_used;
}

//出库操作
void Stock_out(struct DATA_TYPE*pDataBase, int& num_used)
{
	char StockOutName[20];
	printf("请输入想要出货的货物号:\n");
	scanf("%s", StockOutName);

	for (int i = 0; i < num_used; i++)
	{
		if (!strcmp(pDataBase[i].No, StockOutName))
		{
			int StockOutNum = 0;
			printf("已找到想要出货的货物，目前库存为%d件,请输入出货数量:\n",pDataBase[i].inventory);
			scanf("%d", &StockOutNum);
			pDataBase[i].inventory -= StockOutNum;

			if (pDataBase[i].inventory <= 0)
			{
				printf("货物已清空\n");
				pDataBase[i].deleted = 1;
			}
			else 
			{
				printf("成功出货%d件\n", StockOutNum);
			}

			printf("Press any to continue...\n");
			_getch();
			return;
		}
	}

	printf("没有找到货物号为%s的货物\n", StockOutName);
}

//显示库存信息
int Stock_list(struct DATA_TYPE* pDataBase, int &num_used) 
{
	printf("货物号   货物名  库存量   产家名   价格    生产日期    入库日期    经手人名字\n");
	for (int i = 0; i < num_used ; i++)
	{
		printf("%s\n", &pDataBase[i].No);
	}
	return 0;
}

void SumPrice(struct DATA_TYPE* pDataBase, int& num_used)
{
	int sum_price = 0;
	for (int i = 0; i < num_used; i++)
	{
		if (pDataBase[i].deleted == 0)
		{
			sum_price += (pDataBase[i].price * pDataBase[i].inventory);
		}
	}

	printf("仓库里所有商品的总价格为: %d\n", sum_price);
	printf("Press any to continue...\n");
	_getch();
	return;
}